<?php

namespace App\Admin\Forms;


use App\Models\SwtGoodQuantity;
use App\Models\SwtMachine;
use App\Models\SwtReplenishment;
use App\Models\SwtReplenishmentGoods;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;

class CompleteReplenishmentForm extends Form implements LazyRenderable
{
    use LazyWidget;
    public function handle(array $input)
    {
        $id = $this->payload['id'];

        Db::beginTransaction();
        $num = 0;
        try {
            SwtReplenishment::where('id',$id)->update(['replenishment_status'=>2,'back_image'=>$input['back_image']]);
            foreach ($input['goods'] as $value) {

                $res = SwtReplenishmentGoods::where(['replenishment_id'=>$id,'good_id'=>$value['id']]) -> update(['back_quantity' => $value['back_quantity']]);
                $goods_res = SwtGoodQuantity::where(['goods_id'=>$value['id']]) -> increment(['normal_quantity' => $value['back_quantity']]);

                if(!$res || $goods_res) $num++;
            }

            if($num > 0) {
                throw new \Exception('编辑失败');
            }
            //TODO 增加一个请求 商城接口 扣商户押金

            Db::commit();
            return $this->response()->success('编辑成功')->refresh();
        } catch (\Exception $e) {
            Db::rollBack();
            return $this->response()->error('编辑失败');
        }
    }


    public function form()
    {
        $id = $this->payload['id'] ?? null;


        $this->text('replenishment_no', '补货单号')->disable();
        $this->text('replenishment_status', '状态')->disable();
        $this->text('machine_name', '设备')->disable();

        $this -> table('goods','商品明细', function(NestedForm $table) {
            $table -> hidden('id');
            $table -> display('goods_name', '商品名');



            $table -> display('actual_quantity', '实际出库数量');
            $table -> text('back_quantity','回库数量')
                -> type('number')
                -> rules('required', [ 'required' => '数量不能为空'])
                -> attribute('min', 0)
                -> required();

        })->disableCreate()->disableDelete()->default($data['goods'] ?? [])-> setFieldClass(['table-text-center']);

        $this->image('back_image', '回库照片')->move('upload/replenishment');

    }

    public function default()
    {
        $id = $this->payload['id'];

        $replenishment_info = SwtReplenishment::where('id', $id)->first();
        $goods_info  = SwtReplenishmentGoods::with('goods') -> where('replenishment_id', $id)->get()->toArray();
        $machine = SwtMachine::where('id',$replenishment_info->replenishment_machine_id)->first();
        $status_array = ['未出库','出库','补货完成'];

        $goods = [];

        foreach($goods_info as $key=>$value){
            $goods[$key] = [
                'id' => $value['good_id'],
                'goods_name' => $value['goods']['goods_name'],
                'replenishment_quantity' => $value['replenishment_quantity'],
                'actual_quantity' => $value['actual_quantity']
            ];
        }




        $data = [
            'replenishment_no' => $replenishment_info->replenishment_no,
            'replenishment_status' => $status_array[$replenishment_info->replenishment_status],
            'machine_name' => !empty($machine->machine_name) ? $machine->machine_name : '',
            'goods' => $goods,
        ];
        return $data;
    }
}
